<?php
namespace app\modules\app\models;

use app\modules\cn\models\Content;
use app\modules\cn\models\ExerciseListDetail;
use app\modules\cn\models\MockCate;
use app\modules\cn\models\QuestionRemarks;
use app\modules\cn\models\UserDiscuss;
use yii\db\ActiveRecord;
use app\libs\GoodsPager;
use app\libs\Pager;

class QuestionCollect extends ActiveRecord
{
    public $cateData;

    public static function tableName()
    {
        return '{{%user_question_collect}}';
    }

    public static function isCollect($pid, $typeid)
    {
        $uid = \Yii::$app->session->get('uid');
        $data = QuestionCollect::find()->asArray()->where("qid in ($pid) and typeid=$typeid and uid=" . $uid)->one();
        return $data == false ? 0 : 1;
    }

    public static function getCollectQuestion($where, $page, $pageSize=10)
    {
        $offset = ($page - 1) * $pageSize;
        $data = QuestionCollect::find()->from('x2_user_question_collect uqc')->distinct()->select('el.name,el.description,uqc.*,uqc.qid as pid,el.problemComplement')->asArray()->leftjoin('x2_exercise_list el', 'el.content_id=uqc.qid')->where("$where")->offset($offset)->limit($pageSize)->orderBy('id desc')->all();//获取所有的错题
        foreach ($data as $k => $v) {
            $tem['problemComplement']='';
            if($v['typeid']==1){
                $tem=ExerciseListDetail::find()->asArray()->where('pid='.$v['pid'])->orderBy('id asc')->one();
            }
            $data[$k]['description'] = $v['typeid'] != 1 ? $v['description'] : $tem['problemComplement'];
            $mockCate = MockCate::find()->asArray()->where(" `read` like '%".$v['pid']."%' or listen like '%".$v['pid']."%' or `write` like '%".$v['pid']."%'")->one();
            $data[$k]['title']='雅思机考' . ($v['typeid'] == 1 ? '听力' : ($v['typeid'] == 5 ? '全套' : '阅读')) . '模拟卷' . \Yii::$app->params['noToStr'][$mockCate['id']];
        }
        return $data;
    }

    public static function mockIsCollect($pid)
    {
        $uid = \Yii::$app->session->get('uid');
        $contentId = QuestionRemarks::getAllContentId($pid);
        $data = self::find()->asArray()->where('qid in (' . $contentId . ') and uid=' . $uid)->all();
        return $data == false ? 0 : 1;
    }


}